package com.mrobuy.jd.category;

import com.gavel.database.SQLExecutor;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jdvc.entity.VCCategory;
import com.mrobuy.jdvc.entity.VCFeature;
import com.mrobuy.util.MD5Utils;

import java.util.List;

public class VCFeatureLoader {

    public static void main(String[] args) throws Exception {



        List<VCCategory> categories = SQLExecutor.executeQueryBeanList("select * from VC_CATEGORY", VCCategory.class);

        for (VCCategory category : categories) {

            System.out.println(category);

            load(JdClientFactory._appKey, category.getId(), JdClientFactory.jdClient());

        }

    }

    public static void load(String appKey, int cid) throws Exception {
        load(appKey, cid, JdClientFactory.jdClient());
    }

    public static VCFeature load(String appKey, int cid, JdClient client) throws Exception {


        VCFeature vcFeature = Jos.featureFindClient(client)
                .setCid3(cid)
                .call();

        String id = MD5Utils.md5Hex(appKey + "_" + cid).trim().toUpperCase();
        VCFeature exist = SQLExecutor.executeQueryBean("select * from VC_FEATURE where ID = ?", VCFeature.class, id);

        if ( exist==null ) {
            SQLExecutor.insert(vcFeature);
        }

        return vcFeature;
    }


}
